package de.softwareforge.published.proxy;

/*
 * Copyright (C) 2007-2008 Henning P. Schmiedehausen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.sun.javadoc.Doc;

import de.softwareforge.published.doclet.PublishedDoclet;
import de.softwareforge.published.util.StatusCalculator;

/**
 * The proxy controller for the Published Doclet code. This controls the behaviour
 * of the ProxyFactory when a specific object is encountered.
 *
 * @author <a href="mailto:henning@schmiedehausen.org">Henning P. Schmiedehausen</a>
 * @version $Id: PublishedDocletProxyController.java 102 2008-02-24 22:14:11Z hps@intermeta.de $
 * @api.level unpublished
 */
public class PublishedDocletProxyController
        implements ProxyController
{
    private final Map<String, Invoker> methodMap = new HashMap<String, Invoker>();

    private final StatusCalculator statusCalculator;

    private boolean excludeMode = false;

    /**
     * Construct a new ProxyController for a PublishedDoclet instance.
     *
     * @param doclet The Doclet used by this controller.
     */
    public PublishedDocletProxyController(final PublishedDoclet doclet)
    {
        this.statusCalculator = doclet.getStatusCalculator();

        methodMap.put("tags", statusCalculator.new TagsInvoker());
    }

    /**
     * Sets the value of the exclude mode property.
     *
     * @param excludeMode The new exclude mode value.
     */
    public void setExcludeMode(final boolean excludeMode)
    {
        this.excludeMode = excludeMode;
    }

    /**
     * @see ProxyController#getInvokers()
     */
    public Map<String, Invoker> getInvokers()
    {
        return Collections.unmodifiableMap(methodMap);
    }

    /**
     * @see ProxyController#vetoAddToArray(Object, Class)
     */
    public boolean vetoAddToArray(final Object obj, final Class componentType)
    {
        if (obj instanceof Doc)
        {
            if (excludeMode && !statusCalculator.isVisible((Doc) obj))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * @see ProxyController#vetoWrapInProxy(Object)
     */
    public boolean vetoWrapInProxy(final Object obj)
    {
        Class clazz = obj.getClass();
        return !clazz.getName().startsWith("com.sun");
    }
}
